package com.bizmda.bizsip.source.config;

import com.bizmda.bizsip.source.Source;
import com.bizmda.bizsip.config.SourceConfigMapping;
import com.bizmda.log.trace.RestTemplateTraceInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

/**
 * @author 史正烨
 */
@Slf4j
@Configuration
public class SourceConfiguration {
    @Value("${bizsip.config-path:#{null}}")
    private String configPath;

    @Bean
    public SourceConfigMapping sourceConfigMapping() {
        return new SourceConfigMapping(this.configPath);
    }

    @Bean
    @Scope("prototype")
    public Source source() {
        return new Source();
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate()
    {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setInterceptors(Collections.singletonList(new RestTemplateTraceInterceptor()));
        return restTemplate;
    }
}
